import requests
from requests.exceptions import ReadTimeout,ConnectTimeout,RequestException

try:
    req = requests.get("https://www.baidu.com/",timeout=1)
    print(req.status_code)
except ReadTimeout:
    print('超时异常')
except ConnectTimeout:
    print('连接异常')
except RequestException:
    print('请求异常')
else:
    if req.status_code ==200:
        print('访问正常')
        # 将爬取的网页保存到本地
        fb = open("t.html","wb")
        fb.write(req.content)
        fb.close()
    if req.status_code == 404:
        print('页面不存在')
    if req.status_code == 403:
        print('页面禁止访问')
